package com.emep.changzhi.analyse.service.basics;


import com.emep.changzhi.analyse.entity.basics.SysEmissionIndexInfo;
import com.emep.changzhi.analyse.repository.basics.SysEmissionIndexInfoRepository;
import com.emep.changzhi.analyse.utils.MapUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;
import java.util.Map;

/**
 * @author Wangcc
 * @version V1.0
 * @Description: 排放量指标信息表业务层
 * @date 2018/2/27 17:07
 */
@Slf4j
@Service
public class SysEmissionIndexInfoService  {
    
    @Resource
    private SysEmissionIndexInfoRepository sysEmissionIndexInfoRepository;

    /**
     *
     * 不含查询条件的分页获取数据
     *
     * @param pageNo
     * @param pageSize
     * @return
     * @throws Exception
     */
    public Page<SysEmissionIndexInfo> findAll(Integer pageNo, Integer pageSize){
        try {
            Pageable pageable = new PageRequest(pageNo-1, pageSize);
            return sysEmissionIndexInfoRepository.findAll(pageable);
        } catch (Exception e) {
            log.error(e.getClass().getName()+":"+ e.getMessage());
            log.error(e.getLocalizedMessage());
            return null;
        }
    }

    /**
     *
     * 新增排放量指标信息
     *
     * @param payload
     * @return
     * @throws Exception
     */
    public SysEmissionIndexInfo save(Map<String, Object> payload) {
        SysEmissionIndexInfo SysEmissionIndexInfo = null;
        try {
            SysEmissionIndexInfo SysEmissionIndexInfo1 = (SysEmissionIndexInfo) MapUtils.convertMap(SysEmissionIndexInfo.class,payload);
            SysEmissionIndexInfo = sysEmissionIndexInfoRepository.save(SysEmissionIndexInfo1);
        }catch (Exception e){
            log.error(e.getClass().getName()+":"+ e.getMessage());
            log.error(e.getLocalizedMessage());
        }
        return SysEmissionIndexInfo;
    }


    /**
     *
     * 根据id查询排放量指标信息
     *
     * @param id
     * @return
     */
    public SysEmissionIndexInfo findId(String id){
        SysEmissionIndexInfo SysEmissionIndexInfo = null;
        try {
            SysEmissionIndexInfo = sysEmissionIndexInfoRepository.findSysEmissionIndexInfoById(id);
        }catch (Exception e){
            log.error(e.getClass().getName()+":"+ e.getMessage());
            log.error(e.getLocalizedMessage());
        }
        return SysEmissionIndexInfo;
    }

    /**
     *
     * 根据id 删除排放量指标信息
     *
     * @param id
     */
    public Boolean delete(String id) {
        Boolean result = false;
        try {
            Integer  result1 = sysEmissionIndexInfoRepository.deleteById(id);
            if(result1 == 1){
                result = true;
            }
        }catch (Exception e){
            log.error(e.getClass().getName()+":"+ e.getMessage());
            log.error(e.getLocalizedMessage());
        }
        return result;
    }

    /**
     *查询所有排放量指标信息
     *
     * @return
     */
    public List<SysEmissionIndexInfo> list() {
        List<SysEmissionIndexInfo> SysEmissionIndexInfos = null;
        try {
            SysEmissionIndexInfos =  sysEmissionIndexInfoRepository.findAll();
        }catch (Exception e){
            log.error(e.getClass().getName()+":"+ e.getMessage());
            log.error(e.getLocalizedMessage());
        }
        return SysEmissionIndexInfos;
    }


    /**
     *
     * 分页排放量指标信息
     *
     * @param page 页数
     * @param size 数量
     * @param sysEmissionIndexInfo 参数
     * @return
     */
    public Page<SysEmissionIndexInfo> findBookCriteria(Integer page, Integer size, SysEmissionIndexInfo sysEmissionIndexInfo) {
        //规格定义
        Pageable pageable = new PageRequest(page-1,size , Sort.Direction.ASC, "createDate"); //页码：前端从1开始，jpa从0开始，做个转换
        Page<SysEmissionIndexInfo> studentPage = sysEmissionIndexInfoRepository.findAll(new Specification<SysEmissionIndexInfo>(){
            @Override
            public Predicate toPredicate(Root<SysEmissionIndexInfo> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                Predicate p1 = criteriaBuilder.equal(root.get("areaCode").as(String.class), sysEmissionIndexInfo.getAreaCode());
                query.where(criteriaBuilder.or(p1));
                return query.getRestriction();
            }
        },pageable);
        return studentPage;
    }
}
